package com.zking.controller.hcy;

import com.zking.entity.Item;
import com.zking.entity.Seller;
import com.zking.service.hcy.ItemService;
import com.zking.service.hcy.SellerService;
import com.zking.utils.RequestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/Hcy")
public class HcyController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private ItemService itemService;


    //查询商家的信息
    @RequestMapping(value = "/selectSellerAll", method = RequestMethod.GET)
    public String selectSellerAll(HttpServletRequest request) {

        List<Seller> sellers= sellerService.querySellers();
        request.setAttribute("sellers",sellers);
        return "hcy/seller";
    }


    @RequestMapping(value = "/reg",method =RequestMethod.GET, name = "注册")
    public String reg(String sellerId,String password,String nickName,String name,String telephone,String addressDetail,String linkmanName
            ,String linkmanQq,String linkmanMobile,String linkmanEmail,String licenseNumber,String taxNumber,String orgNumber,String legalPerson
            ,String legalPersonCardId,String bankUser,String bankName,String bankAccount){
        Seller seller = new Seller();
        seller.setSellerId(sellerId);
        seller.setPassword(password);
        seller.setNickName(nickName);
        seller.setName(name);
        seller.setTelephone(telephone);
        seller.setAddressDetail(addressDetail);
        seller.setLinkmanName(linkmanName);
        seller.setLinkmanQq(linkmanQq);
        seller.setLinkmanMobile(linkmanMobile);
        seller.setLinkmanEmail(linkmanEmail);
        seller.setLicenseNumber(licenseNumber);
        seller.setTaxNumber(taxNumber);
        seller.setOrgNumber(orgNumber);
        seller.setLegalPerson(legalPerson);
        seller.setLegalPersonCardId(legalPersonCardId);
        seller.setBankUser(bankUser);
        seller.setBankName(bankName);
        seller.setBankAccount(bankAccount);
        seller.setStatus("0");
        //加密类型
        String hashAlgorithmName = "SHA1";
        //需要加密的密码
        Object credentials = seller.getPassword();
        //随机生成的盐值
        String saltStr = UUID.randomUUID().toString().replaceAll("\\-","");
        //用户名作为盐值，用户名是唯一，配上密码就是唯一的。
        Object salt = ByteSource.Util.bytes(saltStr);
        //Object salt = null;
        int hashIterations = 256;
        //生成一个新的密码
        Object newPwd = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
        //设置新密码到用户中
        seller.setPassword(newPwd.toString());
        seller.setMobile(saltStr);

        Boolean b = sellerService.addSeller(seller);
        System.out.println(b);
        return "hcy/shoplogin";
    }

//登录
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String  submitLogin(String sellerId,String password,HttpServletRequest request){


         try {
             UsernamePasswordToken token = new UsernamePasswordToken(sellerId,password);

             Subject subject = SecurityUtils.getSubject();

                //用户登陆的用户名和密码，是否正确
             if (!subject.isAuthenticated()) {
                 subject.login(token);
                 //没有操作的情况下，多少毫秒之后T出登陆
                 //shiro会默认拿这个数除以1000.默认去掉小数。
                 //不能设置为0
                 //永久有效，设置为-1000以上都行
                 //设置再登陆之后
                 //默认是30分钟1800000毫秒
                 subject.getSession().setTimeout(1000000);//毫秒为单位
                 Seller seller = (Seller) subject.getPrincipal();
                 //记住密码
                 token.setRememberMe(true);

             }

         } catch (DisabledAccountException e) {
             request.setAttribute("msg", "账户已被禁用");
             return "hcy/shoplogin";
         } catch (AuthenticationException e) {
             request.setAttribute("msg", "用户名或密码错误");
             return "hcy/shoplogin";
         }

        // 执行到这里说明用户已登录成功
        Seller seller1 = sellerService.querySellers(sellerId);
        request.setAttribute("seller1",seller1);
        System.out.println(seller1);
        return "hcy/index";
    }

    @RequestMapping("/data")
    @ResponseBody
    public Seller getData(String sellerId) {
        Seller seller = sellerService.querySellers(sellerId);
        System.out.println("s==="+seller);
        return seller;
    }

    @RequestMapping(value = "/toShoplogin")
    public String  toShoplogin(){
        return "hcy/shoplogin";
    }

    @RequestMapping(value = "/toIndex")
    public String  toIndex(){
        String username = "未登录";
        //获取当前的用户
        Seller currentLoginUser = RequestUtils.currentLoginSeller();

        if (currentLoginUser != null && StringUtils.isNotEmpty(currentLoginUser.getSellerId())) {
            username = currentLoginUser.getSellerId();
        } else {
            return "redirect:/Hcy/toShoplogin";
        }
        return "hcy/index";
    }
    //注册运营商
    @RequestMapping(value = "/toRegister")
    public String  toRegister(){
        return "hcy/register";
    }


//控制面板
    @RequestMapping(value = "/toHome")
    public String toHome(){
        return "hcy/home";
    }

//运用商修改密码
    @RequestMapping(value = "/toPassword")
    public String toPassword     (){
        return "hcy/password";
    }

    @RequestMapping(value = "/updateSeller", method = RequestMethod.GET,name = "更新商家个人信息" )
    public String updateSeller(HttpServletRequest request) {
        String sellerId = request.getParameter("sellerId");
        String name2 = request.getParameter("name2");
//        String telephone3 = request.getParameter("telephone3");
        String telephone4 = request.getParameter("telephone4");
        String addressDetail2 = request.getParameter("addressDetail2");
        String linkmanName2 = request.getParameter("linkmanName2");
        String linkmanQq2 = request.getParameter("linkmanQq2");
        String linkmanMobile2 = request.getParameter("linkmanMobile2");
        String linkmanEmail2 = request.getParameter("linkmanEmail2");
        String licenseNumber2 = request.getParameter("licenseNumber2");
        String taxNumber2 = request.getParameter("taxNumber2");
        String orgNumber2 = request.getParameter("orgNumber2");
        String legalPerson2 = request.getParameter("legalPerson2");
        String legalPersonCardId2 = request.getParameter("legalPersonCardId2");
        String bankUser2 = request.getParameter("bankUser2");
        String bankName2 = request.getParameter("bankName2");
        String bankAccount2 = request.getParameter("bankAccount2");
        boolean b = sellerService.updateSeller(sellerId, name2, telephone4,addressDetail2,linkmanName2
                ,linkmanQq2,linkmanMobile2,linkmanEmail2,licenseNumber2,taxNumber2,orgNumber2,legalPerson2
                ,legalPersonCardId2,bankUser2,bankName2,bankAccount2);
        System.out.println("a:"+b);
        return "redirect:toIndex";

    }

//运营商修改资料
    @RequestMapping(value = "/toSeller")
    public String toSeller(String sellerId,HttpServletRequest request){

        Seller seller1 = sellerService.querySellers(sellerId);

        request.setAttribute("seller1",seller1);
        System.out.println(seller1);
        return "hcy/seller";
    }


//增加商品
    @RequestMapping(value = "/toGoods")
    public String toGoods(){
        return "hcy/goods";
    }

//规格管理
    @RequestMapping(value = "/toGoods_edit")
    public String toGoods_edit(){
        return "hcy/goods_edit";
    }

    @RequestMapping("/checkSellerId")
    @ResponseBody
    public Seller checkSellerId(String sellerId){
        Seller seller = sellerService.querySellerBySellerId(sellerId);
        return seller;
    }
}
